/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    FanZhiwen
 * 创建时间:    2023-09-25
 */
package org.jeecg.modules.flow.callback.delivery.in;
import cn.hutool.core.util.StrUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.business.warehouse.entity.WmsInReceiptTask;
import org.jeecg.modules.business.warehouse.entity.vo.WmsInReceiptTaskPage;
import org.jeecg.modules.business.warehouse.service.IWmsInReceiptTaskService;
import org.jeecg.modules.flow.callback.enums.DocumentStatusEnum;
import org.jeecg.modules.flow.sys.common.BaseCustomBeanRun;
import org.jeecg.modules.flow.sys.entity.dto.BaseFlowDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库任务编辑和保存回调前bean
 *
 * @author FanZhiwen
 * @version 1.0.0
 * @since 2023-09-25
 */
@Component
public class R105InBoundTaskDBRKSaveBean<T> extends BaseCustomBeanRun<T> {
    @Autowired
    private IWmsInReceiptTaskService iWmsInReceiptTaskService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void actureRun() {
        BaseFlowDto baseFlowDto = this.getBaseFlowDto();
        // 转class.自定义class,data对应的class
        baseFlowDto.translateClass(WmsInReceiptTaskPage.class);
        WmsInReceiptTaskPage data = (WmsInReceiptTaskPage) baseFlowDto.getData();
        WmsInReceiptTask wmsInReceiptTask = new WmsInReceiptTask();
        BeanUtils.copyProperties(data, wmsInReceiptTask);
        //草稿状态为待提交
        data.setDocumentStatus(DocumentStatusEnum.SAVE.getCode());
        if (StrUtil.isNotBlank(data.getId())){
            iWmsInReceiptTaskService.updateMain(wmsInReceiptTask,data.getWmsInReceiptTaskMaterialList());
            baseFlowDto.setResult(Result.OK("编辑成功"));
        }else {
            String id = iWmsInReceiptTaskService.saveMain(wmsInReceiptTask, data.getWmsInReceiptTaskMaterialList());
            // id是保存业务记录的id。用于生成业务流程信息记录
            baseFlowDto.setInfoId(id);
            baseFlowDto.setResult(Result.OK(id));
        }
    }
}
